<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= menuPageGroupMod();
	$module = sxREQUEST("module");
	$menuPage = sxREQUEST("menu_page");
	$menuGroup = sxREQUEST("menu_group");
	$newGroup = sxREQUEST("new_group");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$sequence = sxREQUEST("sequence");;
	$flagDisplay = sxREQUEST("flag_display");;
} else {
	if(!isset($_REQUEST["mod"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'mod' non passata")); 
		serverRedir($url);
	}
	if(!isset($_REQUEST["menu_page"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'menu_page' non passata")); 
		serverRedir($url);
	}
	if(!isset($_REQUEST["menu_group"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'menu_group' non passata")); 
		serverRedir($url);
	}
	$module = sxREQ('mod');
	$menuPage = sxREQ('menu_page');
	$menuGroup = $newGroup = sxREQ('menu_group');
	$sSQL = "SELECT mg.*, m.description AS mdes, mp.description AS pdes FROM menu_group mg " .
			" INNER JOIN module m ON m.module = mg.module " .
			" INNER JOIN menu_page mp ON mp.module = mg.module AND mp.menu_page = mg.menu_page" .
			" WHERE mg.module = '$module' AND mg.menu_page = '$menuPage' AND mg.menu_group = '$menuGroup' ";
	$objSDAO->query($sSQL,"mod");
	if (!$row=$objSDAO->fetch("mod")) {
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".
			urlencode(_t("gruppo voci men@ '%s'inesistente",$module.'.'.$menuPage.'.'.$menuGroup)); 
		serverRedir($url);
	}
	$description = $row['description'];
	$note = $row['note'];
	$sequence = $row['sequence'];
	$flagDisplay = $row['flag_display'];
}
//output HTML
$aOpz = array("tit2"=>_t("Crereqtype nuova gruppo voci pagina men@"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			objFrm.submit();
		}catch(e){
			raiseError(e.description, "save new sub-module");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"menuPageGroupMod.php\" method=\"POST\" enctype=\"multipart/form-data\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">\n");
echo("<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module\">\n");
echo("<input type=\"hidden\" name=\"menu_page\" id=\"menu_page\" value=\"$menuPage\">\n");
echo("<input type=\"hidden\" name=\"menu_group\" id=\"menu_group\" value=\"$menuGroup\">\n");
//output comandi
$aCmd = array("insMP"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("conferma inserimento nuova pagina men@"),
										"text"=>_t("conferma")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>"cancel inserimento",
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='4'>"._t("Modulo").": $module - ".''."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>"._t("codice")." (*)</td>" .
			"<td class=\"text01\" width='10%'>" .
			"<input class=\"inpt01\" type=\"text\" name=\"new_group\" id=\"new_group\" maxlength=\"4\" value=\"$newGroup\" size='4'></td>" .
			"<td class=\"labl01\" width='10%'>"._t("sequenza")." (*)</td>" .
			"<td class=\"text01\" width='60%'><input class=\"inpt01\" type=\"text\" name=\"sequence\" id=\"sequence\" maxlength=\"4\" value=\"$sequence\" size='4'></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")." (*)</td>" .
			"<td class=\"text01\" colspan='3'><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\" colspan='3'><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">$note</textarea></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Visualizza")."</td>" .
			"<td class=\"text01\" colspan='3'>" .
			"<input class=\"inpt01\" type=\"radio\" name=\"flag_display\" id=\"flag_display\" value='Y' ".
			(($flagDisplay=='Y')?'CHECKED':'').">" ._t('yes')."&nbsp;&nbsp;&nbsp;".
			"<input class=\"inpt01\" type=\"radio\" name=\"flag_display\" id=\"start_page\" value='N' ".
			(($flagDisplay!='Y')?'CHECKED':'').">" ._t('no')."&nbsp;&nbsp;&nbsp;".
			"</td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));
echo("<div id=\"messArea\" class=\"mess01\">$errMsg</div>");
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

function menuPageGroupMod(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["new_group"] = strtolower($_REQUEST["new_group"]);
	if(ereg("[^a-z0-9]{4}",$_REQUEST["new_group"]))
		$errMsg .= _t("Il nome pagina men@ deve essere un alfanumerico di 4 caratteri").".<br>";
	if(ereg("[^0-9]",$_REQUEST["sequence"]))
		$errMsg .= _t("la sequenza deve essere un numero").".<br>";
	if(trim($_REQUEST["description"]) == '')
		$errMsg .= _t("La description &egrave; obbligatoria.")."<br>";
	//controllo che il modulo non esista gi@
	if (sxREQUEST('menu_group') != sxREQUEST('new_group')) {
		$sSQL = "SELECT count(*) as num FROM menu_group " .
				" WHERE module='".sxRAS("module")."' " .
					" AND menu_page = '".sxRAS("menu_page")."'".
					" AND menu_group = '".sxRAS("new_group")."'";
		$objSDAO->query($sSQL,"mod");
		$row = $objSDAO->fetch("mod");
		if($row["num"] != 0)
			$errMsg .= _t("Il gruppo voci di pagina men@ esiste gi&agrave;.<br>");
	}
	if($errMsg != ""){ return $errMsg; }
	$sSQL = "UPDATE menu_group SET " .
			" description = '".sxRAS("description")."', " .
			" sequence = ".intval(sxRAS("sequence")).", " .
			" flag_display = '".sxRAS("flag_display")."', " .
			" note = '".sxRAS("note")."' " .
			" WHERE module='".sxRAS("module")."' " .
				" AND menu_page = '".sxRAS("menu_page")."'".
				" AND menu_group = '".sxRAS("new_group")."'";
	$objSDAO->query($sSQL,"mod");
	if (sxREQUEST('menu_group') != sxREQUEST('new_group')) {
		$sSQL = "UPDATE menu_group SET menu_group = '".sxRAS("new_group")."' " .
				" WHERE module='".sxRAS("module")."' " .
					" AND menu_page = '".sxRAS("menu_page")."'".
					" AND menu_group = '".sxRAS("menu_group")."'";
		$objSDAO->query($sSQL,"mod");
		$sSQL = "UPDATE menu_line SET menu_group = '".sxRAS("new_group")."' " .
				" WHERE menu.line LIKE ='".sxRAS("module").".".sxRAS("menu_page").".%' " .
						"AND menu_group = '".sxRAS("menu_group")."'";
		$objSDAO->query($sSQL,"mod");
	}
	
	logRecord("300", _t("Modificato gruppo voci men@ '".sxREQUEST('module').
		"/".sxREQUEST('menu_page')."/".sxREQUEST('menu_group')."'"));
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit;
}

?>
